home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / News / Alexandra.0.82 / Source / MatrixSet.m < prev    next >
Encoding:
Text File  |  1996-01-30  |  2.6 KB  |  140 lines

  1. #import "MatrixSet.h"
  2. #import "ColumnMatrix.h"
  3. #import "instr.h"
  4. #import "regexpr.h"
  5. #import "Article.h"
  6.  
  7. @implementation MatrixSet
  8.  
  9. - init
  10. {
  11.    [super init];
  12.    currentSelection=nil;
  13.    numSelCells=0;
  14.    return self;
  15. }
  16.  
  17. - theMatrix
  18. {
  19.    return myMatrix;
  20. }
  21.  
  22. - (List *)matrixList
  23. {
  24.    return myMList;
  25. }
  26.  
  27. - (int)numSelCells
  28. {
  29.    return numSelCells;
  30. }
  31.  
  32. - redisplayMatrix
  33. {
  34.    [myMatrix display];
  35.    return self;
  36. }
  37.  
  38. - currentSelection
  39. {
  40.    return currentSelection;
  41. }
  42.  
  43.  
  44.  
  45. - sync
  46. {
  47.    [self syncAndReturnList:NO];
  48.    return self;
  49. }
  50.  
  51. - syncAndReturnList:(BOOL)flag
  52. {
  53.    List *selectionList=[myMatrix getCurrSelections];
  54.    int oldNumSelCells=numSelCells;
  55.  
  56.    numSelCells=[selectionList count];
  57.    if(numSelCells==1)
  58.       currentSelection=[selectionList objectAt:0];
  59.    else
  60.       currentSelection=nil;
  61.  
  62.    if((oldNumSelCells==1)&&(numSelCells!=1))
  63.       [unselTarget perform:unselAction];
  64.  
  65.    if(flag==FALSE){
  66.       [selectionList free];
  67.       return self;
  68.    }
  69.  
  70.    return selectionList; 
  71. }
  72.  
  73. - (const char *)stringValueForCellAt:(int)index
  74. {
  75.    return [[myMList objectAt:index] stringValue];
  76. }
  77.  
  78. - (int)grep:(const char *)pattern regexpr:(BOOL)regexpr cases:(BOOL)cases
  79. {
  80.    int i,j;
  81.    List *selList;
  82.    unsigned char fm[256], tr[256];
  83.    struct re_pattern_buffer rpat;
  84.  
  85.    selList=[[List alloc] init];
  86.    j=[myMList count];
  87.    if(j==0)
  88.       return 0;
  89.  
  90.    if(regexpr){
  91.       char *str;
  92.       memset(&rpat, 0, sizeof(rpat));
  93.       for(i=256; i--;)
  94.          tr[i] = i;
  95.       if(!cases)
  96.          for(i='A'; i<='Z'; i++) tr[i] = i-'A'+'a';
  97.       rpat.translate = tr; rpat.fastmap = fm;
  98.       str = re_compile_pattern((char *)pattern,strlen(pattern), &rpat);
  99.       if (str!=NULL)
  100.         return (strcmp(str, "Out of memory")?SEARCH_INVALID_REGEXPR:SEARCH_INTERNAL_ERROR);
  101.    }
  102.  
  103.    for(i=0;i<j;i++){
  104.       const char *cellString=[self stringValueForCellAt:i];
  105.       BOOL found=FALSE;
  106.  
  107.       if(regexpr){
  108.         int l=strlen(cellString);
  109.         int p=re_search_pattern(&rpat,(char *)cellString,l,0,l,0);
  110.  
  111.         if(p==-2)
  112.            return SEARCH_INTERNAL_ERROR;
  113.         found= (p==-1)? FALSE : TRUE;
  114.       }
  115.       else
  116.          found=(instr(cellString,pattern,cases)!=NULL);
  117.  
  118.       if(found)
  119.          [selList addObject:[myMList objectAt:i]];
  120.    }
  121.  
  122.    j=[selList count];
  123.    if(j>0){
  124.       [myMList makeObjectsPerform:@selector(unsetTag)];
  125.       [selList makeObjectsPerform:@selector(setTag)];
  126.       for(i=0;i<j;i++)
  127.          if([[selList objectAt:i] isTaged]){
  128.             [[myMatrix reloadMatrix] display];
  129.             [self sync];
  130.             break;
  131.          }
  132.       if(i==j) j=0; //articles may be killed
  133.    }
  134.    [selList free];
  135.  
  136.    return j;
  137. }
  138.  
  139. @end
  140.